//
//  BaseTabBarViewController.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/10/31.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "BaseTabBarViewController.h"


#import "CTTabBarItem.h"

#import "HomeViewController.h"
#import "MessageViewController.h"
#import "PushViewController.h"
#import "ContactViewController.h"
#import "MeViewController.h"


#import "Common.h"

//#define kTabbarH 49

//#define kTabbarH      SCREENH>736?100:50

#define kBtnW (SCREENW/5)
//#define KBtnH 49
#define KBtnH 50


@interface BaseTabBarViewController ()<UITabBarControllerDelegate,UINavigationControllerDelegate>{
   NSInteger selectIndex;
    BOOL     tabBarIsShow;
}
@property (nonatomic,strong) UIButton *button;
@end

@implementation BaseTabBarViewController

- (instancetype)init {
    if(self = [super init]) {
        selectIndex = 0;
        tabBarIsShow = YES;
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    selectIndex = 0;
    tabBarIsShow = YES;
    

    
    //隐藏系统uitabbar
    [self hideSystemTabbar];
    //创建自定义的tabbar
    [self createCustomTabbar];
    //添加viewController
    [self setTabBarVC];
    //添加中心
    [self setup];
    //设置通知
    [self addButtonNotifation];
}

//添加大圆按钮的通知
-(void)addButtonNotifation{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideTabBar) name:@"kTabbarCenterHideNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showTabBar) name:@"kTabbarCenterShowNotification" object:nil];
}

// 初始化所有子控制器
- (void)setTabBarVC{
   
    HomeViewController *homeVC = [[HomeViewController alloc]init];
    MessageViewController *secVC = [[MessageViewController alloc]init];
    UIViewController *three = [[UIViewController alloc]init];
    ContactViewController *fourVC = [[ContactViewController alloc]init];
    MeViewController *fiveVC = [[MeViewController alloc]init];
    NSArray *vcs = @[homeVC,secVC,three,fourVC,fiveVC];
    NSMutableArray *ncArr = [[NSMutableArray alloc]init];
    for(int i = 0;i<vcs.count;i++){
        UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:[vcs objectAtIndex:i]];
        nc.delegate = self;
        [ncArr addObject:nc];
        
        [self addChildViewController:nc];
    }
    
    
}


//// 添加tabbar的子viewcontroller
//- (void)setTabBarChildController:(UIViewController*)controller title:(NSString*)title image:(NSString*)imageStr selectImage:(NSString*)selectImageStr{
//
//
//    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller];
//    nav.delegate = self;
//    [self addChildViewController:nav];
//
//
//}



#pragma mark- setup
-(void)setup
{
    //  添加突出按钮
    [self addCenterButtonWithImage:[UIImage imageNamed:@"tabbar_2"] selectedImage:[UIImage imageNamed:@"tabbar_2_sele"]];
}
#pragma mark - addCenterButton
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage selectedImage:(UIImage*)selectedImage
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button addTarget:self action:@selector(pressChange:) forControlEvents:UIControlEventTouchUpInside];
    self.button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    
    //  设定button大小为适应图片
    self.button.frame = CGRectMake(0, 0.0, kBtnW, KBtnH);
    [self.button setImage:buttonImage forState:UIControlStateNormal];
    [self.button setImage:selectedImage forState:UIControlStateSelected];
    
    //  这个比较恶心  去掉选中button时候的阴影
    self.button.adjustsImageWhenHighlighted=NO;
    /*
     *  核心代码：设置button的center 和 tabBar的 center 做对齐操作， 同时做出相对的上浮
     */
    CGPoint center = self.tabBarBg.center;
    center.y = SCREENH>736?(center.y - buttonImage.size.height/2):center.y;
    self.button.center = center;
    [self.view addSubview:self.button];
    
    
}

-(void)pressChange:(id)sender
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"PUSHMESSAGE" object:nil];
}


-(void)praiseBtnTapped{
   
}
#pragma mark 首页刷新
//双击首页
-(void)praiseBtnRepeatTapped{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"freshHomeMsg" object:nil];
}
//创建自定义的tabbar
- (void)createCustomTabbar {
    
    self.tabBarBg = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENH-(SCREENH>736?90:50), SCREENW, (SCREENH>736?90:50))];
    self.tabBarBg.backgroundColor =bgmColor(248, 248, 248);
    
    //画线
    UILabel *labLine=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 0.3)];
    labLine.backgroundColor=[UIColor grayColor];
    labLine.alpha = 0.3;
    [self.tabBarBg addSubview:labLine];
    [self.view addSubview:self.tabBarBg];
    
    //首页
    CTTabBarItem * homeBtn = [[CTTabBarItem alloc]initWithFrame:CGRectMake(0, 0 , kBtnW, KBtnH)];
    homeBtn.tag = 0;
    [homeBtn setNormalImage:[UIImage imageNamed:@"tabbar_0"]
              selectedImage:[UIImage imageNamed:@"tabbar_0_sele"]
                      title:@""]; //
    
    [homeBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    [homeBtn addTarget:self action:@selector(praiseBtnTapped) forControlEvents:UIControlEventTouchDown];
//    [homeBtn addTarget:self action:@selector(praiseBtnRepeatTapped) forControlEvents:UIControlEventTouchDownRepeat];
 
    [self.tabBarBg addSubview:homeBtn];
    homeBtn.selected = YES;
    
    //消息
    CTTabBarItem *msgBtn= [[CTTabBarItem alloc]initWithFrame:CGRectMake(1*kBtnW, 0, kBtnW, KBtnH)];
    msgBtn.tag = 1;
    [msgBtn setNormalImage:[UIImage imageNamed:@"tabbar_1"]
                 selectedImage:[UIImage imageNamed:@"tabbar_1_sele"]
                         title:@"" ];
    [msgBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarBg addSubview:msgBtn];
    

    
    
//    //通y讯录
    CTTabBarItem *addressBtn= [[CTTabBarItem alloc]initWithFrame:CGRectMake(3*kBtnW, 0, kBtnW, KBtnH)];
    addressBtn.tag = 3;
    [addressBtn setNormalImage:[UIImage imageNamed:@"tabbar_3"]
                 selectedImage:[UIImage imageNamed:@"tabbar_3_sele"]
                         title:@"" ];
    [addressBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarBg addSubview:addressBtn];
  
    //我
    CTTabBarItem *meBtn=[[CTTabBarItem alloc]initWithFrame:CGRectMake(4*kBtnW, 0, kBtnW, KBtnH)];
    meBtn.tag = 4;
    [meBtn setNormalImage:[UIImage imageNamed:@"tabbar_4"]
            selectedImage:[UIImage imageNamed:@"tabbar_4_sele"]
                    title:@""];
    [meBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarBg addSubview:meBtn];
}


#pragma mark - 自定义tabbar按钮点击事件
- (void)tabBarBtnClick:(CTTabBarItem *)btn {
    
    if(btn.tag != selectIndex) {
        for(UIView *view in self.tabBarBg.subviews) {
            if([view isKindOfClass:[UIButton class]]) {
                CTTabBarItem *button = (CTTabBarItem *)view;
                button.selected = NO;
            }
        }
        btn.selected = YES;
        selectIndex = btn.tag;
        self.selectedIndex=btn.tag;
    }else{
        if(btn.tag ==0){
            [self praiseBtnRepeatTapped];
        }
        
    }
}


#pragma mark - 隐藏系统的tabbar
- (void)hideSystemTabbar {
    if ( [self.view.subviews count] < 2 ) {
        return;
    }
    
    UIView *contentView;
    if ( [[self.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] ) {
        contentView = [self.view.subviews objectAtIndex:1];
    } else {
        contentView = [self.view.subviews objectAtIndex:0];
    }
    BOOL hide=YES;
    if (hide) {
        contentView.frame = self.view.bounds;
    } else {
        contentView.frame = CGRectMake(self.view.bounds.origin.x,
                                       self.view.bounds.origin.y,
                                       self.view.bounds.size.width,
                                       self.view.bounds.size.height -
                                       self.tabBar.frame.size.height);
    }
    
    for(UIView *view in self.view.subviews) {
        if([view isKindOfClass:[UITabBar class]]) {
            view.hidden = YES;
            break;
        }
    }
}

#pragma mark -公有方法

/**
 *  设置选中tabItem项
 *
 *  @param index 选中的第几个tab
 */
- (void)selectedItemTabBarIndex:(BGMTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (index==button.tag) {
                [self tabBarBtnClick:button];
                break;
            }
        }
    }
}

//显示微章
- (void)showBadgeValue:(NSString*)badgeValue index:(NSInteger)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button showBadgeValue:badgeValue];
                break;
            }
        }
    }
}

//移除微章
- (void)removeBadgeValueWithIndex:(BGMTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button removeBadgeValue];
                break;
            }
        }
    }
}

//显示红点
- (void)showBadgeWithIndex:(BGMTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button showBadge];
                break;
            }
        }
    }
}

//删除红点
- (void)removeBadgeWithIndex:(BGMTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button removeBadge];
                break;
            }
        }
    }
}

- (void)modifyTabBarFrame {
    CGRect r = self.tabBarBg.frame;
    r.origin.y = SCREENH-(SCREENH>736?90:50);
    self.tabBarBg.frame = r;
    self.view.frame = CGRectMake(0, 0, SCREENW, SCREENH-(SCREENH>736?90:50));
}

- (void)restoreTabBarFrame {
    CGRect r = self.tabBarBg.frame;
    r.origin.y = SCREENH-(SCREENH>736?90:50)-64;
    self.tabBarBg.frame = r;
    
    self.view.frame = CGRectMake(0.0f, 0.0f, SCREENW, SCREENH-(SCREENH>736?90:50)-64);
}

- (void)hideTabBar {
    if (!tabBarIsShow)
    {
        return;
    }
//    [UIView animateWithDuration:0.35
//                     animations:^{
                         CGRect tabFrame = self.tabBarBg.frame;
                         tabFrame.origin.x = 0 - tabFrame.size.width;
                         self.tabBarBg.frame = tabFrame;
                         self.button.alpha = 0.0;
//                     }];
    tabBarIsShow = NO;
}

- (void)showTabBar {
    if (tabBarIsShow)
    {
        return;
    }
//    [UIView animateWithDuration:0.35
//                     animations:^{
    
                         CGRect tabFrame = self.tabBarBg.frame;
                         tabFrame.origin.x = CGRectGetWidth(tabFrame) + CGRectGetMinX(tabFrame);
                         self.tabBarBg.frame = tabFrame;
                         self.button.alpha = 1.0;
//                     }];
    tabBarIsShow = YES;
}

#pragma mark -UINavigationControllerDelegate Methods
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

//    if (navigationController.viewControllers.count >1) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"kTabbarCenterHideNotification" object:nil];
//    }
//
//    if (navigationController.viewControllers.count == 1) {
//         [[NSNotificationCenter defaultCenter] postNotificationName:@"kTabbarCenterShowNotification" object:nil];
//    }
    
    
    if ([viewController isKindOfClass:[MeViewController class]] || [viewController isKindOfClass:[HomeViewController class]] || [viewController isKindOfClass:[ContactViewController class]] ||[viewController isKindOfClass:[MessageViewController class]]) {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"kTabbarCenterShowNotification" object:nil];
    }else{
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kTabbarCenterHideNotification" object:nil];

    }
    

}


-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    if (navigationController.viewControllers.count >1) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"kTabbarCenterHideNotification" object:nil];
//    }
//
//    if (navigationController.viewControllers.count == 1) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"kTabbarCenterShowNotification" object:nil];
//    }
    
    
}

@end
